# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=gettext
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.21
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/gettext/"
pkgdesc="GNU internationalization library (mingw-w64)"
# GPL3 for the package as a whole and LGPL for some parts, see the license files
license=(GPL3 partial:'LGPL2.1')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' 'staticlibs')
source=("https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"
	0001-printf.c-guard-the-wprintf-functions-by-the-correct-.patch
        0005-Fix-compilation-of-pthread_sigmask.c.patch
        0010-build-Fix-build-failure-on-mingw-formatstring_ruby.patch
        0011-fix-interference-between-libintl-boost-header-files.patch
        122-Use-LF-as-newline-in-envsubst.patch
        0020-0.21-disable-libtextstyle.patch
        2001-gnulib-open-bug.patch
        2002-gnulib-open-bug.patch)
sha256sums=('c77d0da3102aec9c07f43671e60611ebff89a996ef159497ce8e59d075786b12'
            '16741a6250ba49744b01c9986e7164b25d2143dfcee3eeeb422a5e96f3e6cdd5'
            'cbc2f533012d646521afa20f8b256917fce040741ff42cf53fb6dd7123a6670a'
            'c4a85dc6d781d29bdcdc557d8867408f2181d871338f6a1c4b9e6fcd78dc3e7f'
            '41201d74f5f352c4fe219316340521fc3f9a0f3a7c572a3ddd3b8df33d4dab9a'
            'ef9f11a1cd10539d4323c6fcba3013cc503d47366004fe8a99c642dc3ddf2fd0'
            '544ce0589e9c70f4f75d77c76fd36f88d009ac9cfecb4812a67f878e38ac6418'
            '41cb9fdd466a304688fe4e192e3b381963dd2831a162592f792cc26fdfc16bc7'
            '2b4dbb3de850afa108f82b9bf4e74631e2aa35ae7a9addea23c430ed7e03203d')
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871') #Daiki Ueno

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-printf.c-guard-the-wprintf-functions-by-the-correct-.patch \
    0005-Fix-compilation-of-pthread_sigmask.c.patch \
    0010-build-Fix-build-failure-on-mingw-formatstring_ruby.patch \
    0011-fix-interference-between-libintl-boost-header-files.patch \
    122-Use-LF-as-newline-in-envsubst.patch

  # https://src.fedoraproject.org/rpms/gettext/blob/1b17af56abe8f1ba5df0218a88bb495ce81466b5/f/gettext-0.21-disable-libtextstyle.patch
  apply_patch_with_msg \
    0020-0.21-disable-libtextstyle.patch

  # https://github.com/msys2/MINGW-packages/issues/10520
  # https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=fd47c1834c2bcb8772f7a320ab088d802b1ca649
  # https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=3fcdcac504be1e4d55cbee0fa185b380712545d7
  pushd gettext-tools/gnulib-lib
    patch -Nbp2 -i "${srcdir}/2001-gnulib-open-bug.patch"
    patch -Nbp2 -i "${srcdir}/2002-gnulib-open-bug.patch"
  popd

  libtoolize --automake --copy --force
  WANT_AUTOMAKE=latest ./autogen.sh --skip-gnulib
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  export lt_cv_deplibs_check_method='pass_all'
  export MSYS2_ARG_CONV_EXCL="-DLOCALEDIR=;-DLIBDIR=;-DLOCALE_ALIAS_PATH="

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --disable-java \
    --disable-native-java \
    --disable-csharp \
    --enable-static \
    --enable-threads=win32 \
    --enable-relocatable \
    --without-emacs \
    --disable-openmp \
    --without-cvs \
    --without-git \
    --with-included-libcroco \
    --with-included-libunistring \
    --with-included-libxml \
    --with-included-glib \
    --with-libncurses-prefix=${MINGW_PREFIX} \
    --disable-silent-rules

  # to make relocate() in gnulib-lib work
  sed -s "s|${MINGW_PREFIX}|$(cygpath -m ${MINGW_PREFIX})|g" -i gettext-tools/config.h

  make
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"                                 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/COPYING"                 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-runtime/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/intl/COPYING.LIB"        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-runtime/intl/COPYING.LIB"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/libasprintf/COPYING"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-runtime/libasprintf/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/libasprintf/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-runtime/libasprintf/COPYING.LIB"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-tools/COPYING"                   "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-tools/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-tools/gnulib-lib/libxml/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gettext-tools/gnulib-lib/libxml/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gnulib-local/lib/libxml/COPYING"         "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gnulib-local/lib/libxml/COPYING"
}
